// index page
// ============

define(
    [ 'require', 'zepto', 'wx', 'ejs', 'utils', 'sui' ],
    function(require, $, wx, ejs, utils) {
      return {
        init : function(data) {
          this.initModal();
        },
        
        initModal : function() {
          $(".cash").on('blur', function(){
            this.value = this.value.replace(/[\.]$/, '');
            this.value = this.value.replace(/(.*)\.([\d]{2})(\d*)/g,'$1.$2');
            var value = Number(this.value).toFixed(2);
            this.value = value;
            
            if(value * 100 > parseInt($(this).data("max"))) {
              $.toast("金额输入过大")
              this.value=null;
            }
            var logNum = this.value == null ? null : this.value.toString();
          })
          
            
          $(document).on('click','.draw-button', function () {
            var cashVal = $(".cash").val();
            var max = Number($(".cash").data("max"));
            
            if(cashVal && /^(([1-9]\d*)|\d)(\.\d{1,2})?$/.test(cashVal)) {
              cashVal = Number(cashVal) * 100;
              if(cashVal <= max ) {
                $.modal({
                  title:  '您本次提现的金额为',
                  text: '<p class="price-layout"><span class="price">'+ Number(cashVal/100).toFixed(2) +'</span>元</p>',
                  buttons: [{
                    text: '确认',
                    onClick: function() {

                      $.ajax({
                        type: "POST",
                        url: window.__CTX__.root + "/profile/withdrawal?price=" + cashVal + "&content=提现",
                        dataType: "json",
                        success: function(data) {
                          $.hidePreloader();
                          if(data.code == 0) {
                            window.location.href = window.__CTX__.root + "/profile";
                          } else {
                            $.toast(data.message);
                          }
                        },
                        error: function() {
                          $.toast("创建失败");
                        }
                      })
//                      window.location.href= window.__CTX__.root + "/profile"
                    }
                  },
                  {
                    text: '取消',
                  }],
                  extraClass: "draw-modal"
                })
              } else {
                $.toast("余额不足，请重新输入提现金额");
                $(".cash").val('')
              }
            } else {
            $.toast("请输入提现金额");
          }
          });
        }
      }
    })